package com.hao.user.app.login;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hao.common.code.ErrorCode;
import com.hao.common.execption.BusinessException;
import com.hao.user.app.mapper.UserMapper;
import com.hao.user.app.model.dto.user.UserLoginRequest;
import com.hao.user.app.model.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;

/**
 * @author haoge
 * @version 1.0
 * @date 2023/5/21 21:30
 */
public class AccountLoginStrategy implements LoginStrategy {

    @Override
    public User login(UserLoginRequest userLoginRequest, RedisTemplate<String ,Object> redisTemplate, UserMapper userMapper) {
        String userAccount = userLoginRequest.getUserAccount();
        String password = userLoginRequest.getPassword();
        String SALT = "haoge";
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes());

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("password", encryptPassword);
        queryWrapper.eq("userAccount", userAccount);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        }
        return user;
    }
}
